package cn.tedu.eb_film.service.impl;

import cn.tedu.eb_film.ex.ServiceException;
import cn.tedu.eb_film.mapper.CsRevocationRegulationMapper;
import cn.tedu.eb_film.mapper.OrderMapper;
import cn.tedu.eb_film.pojo.dto.CsRevocationRegulationAddNewDTO;
import cn.tedu.eb_film.pojo.dto.CsRevocationRegulationUpdateDTO;
import cn.tedu.eb_film.pojo.dto.OrderAddNewDTO;
import cn.tedu.eb_film.pojo.dto.OrderUpdateDTO;
import cn.tedu.eb_film.pojo.entity.CinemaHall;
import cn.tedu.eb_film.pojo.entity.CsRevocationRegulation;
import cn.tedu.eb_film.pojo.entity.Order;
import cn.tedu.eb_film.pojo.vo.*;
import cn.tedu.eb_film.service.CsRevocationRegulationService;
import cn.tedu.eb_film.service.OrderService;
import cn.tedu.eb_film.utils.ServiceCode;
import cn.tedu.eb_film.utils.StateCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 退票规制表服务实现类
 *
 * @author zzzzc
 * @since 2023-03-18 18:38:25
 */
@Slf4j
@Service
public class CsRevocationRegulationServiceImpl implements CsRevocationRegulationService {
    @Autowired
    private CsRevocationRegulationMapper mapper;

    public CsRevocationRegulationServiceImpl(){
        log.debug("创建业务类对象：CsRevocationRegulationServiceImpl");
    }

    @Override
    public void addNew(CsRevocationRegulationAddNewDTO csRevocationRegulationAddNewDTO) {
        CsRevocationRegulation csRevocationRegulation = new CsRevocationRegulation();
        BeanUtils.copyProperties(csRevocationRegulationAddNewDTO,csRevocationRegulation);
        log.debug("即将执行插入数据，参数：{}",csRevocationRegulationAddNewDTO);
        csRevocationRegulation.setCreateTime(LocalDateTime.now());
        mapper.insert(csRevocationRegulation);
    }


    @Override
    public void delete(Integer id) {
        log.debug("开始处理【根据ID删除退票规制】的业务，参数：{}", id);
        CsRevocationRegulationStandardVO csRevocationRegulationStandardVO = mapper.getStandardById(id);
        log.debug("根据ID={}检查询退票规制数据是否存在，查询结果：{}", id, csRevocationRegulationStandardVO);
        if (csRevocationRegulationStandardVO == null||csRevocationRegulationStandardVO.getState().equals(StateCode.STATE_DELETE.getValue())){
            String message = "删除失败，该退票规制不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        mapper.deleteById(id);

    }

    @Override
    public void setEnable(Integer id) {
        updateEnableById(id,1);
    }

    @Override
    public void setDisable(Integer id) {
        updateEnableById(id,0);
    }

    private void updateEnableById(Integer id,Integer enable) {
        CsRevocationRegulationStandardVO queryResult = mapper.getStandardById(id);
        if (queryResult == null||queryResult.getState().equals(StateCode.STATE_DELETE.getValue())){
            String message = ENABLE_TEXT[enable]+"数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        if (enable == queryResult.getState()){
            String message = ENABLE_TEXT[enable]+"失败,修改的目标状态与目前一致";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        CsRevocationRegulation csRevocationRegulation = new CsRevocationRegulation();
        csRevocationRegulation.setId(id);
        csRevocationRegulation.setState(enable);

        int rows = mapper.update(csRevocationRegulation);
        if (rows != 1) {
            String message = ENABLE_TEXT[enable] + "失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }

    }


    @Override
    public void updateInfoById(Integer id, CsRevocationRegulationUpdateDTO csRevocationRegulationUpdateDTO) {
        CsRevocationRegulationStandardVO queryResult = mapper.getStandardById(id);
        if (queryResult == null||queryResult.getState().equals(StateCode.STATE_DELETE.getValue())){
            String message = "数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        CsRevocationRegulation csRevocationRegulation = new CsRevocationRegulation();
        BeanUtils.copyProperties(csRevocationRegulationUpdateDTO,csRevocationRegulation);
        csRevocationRegulation.setId(id);
        mapper.update(csRevocationRegulation);
    }

    @Override
    public CsRevocationRegulationStandardVO getStandardById(Integer id) {
        log.debug("开始处理【根据退票规制id查询详情】的业务，ID：{}", id );
        CsRevocationRegulationStandardVO csRevocationRegulationStandardVO = mapper.getStandardById(id);
        if (csRevocationRegulationStandardVO==null||csRevocationRegulationStandardVO.getState().equals(StateCode.STATE_DELETE.getValue())){
            String message = "查询失败，数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return csRevocationRegulationStandardVO;
    }

    @Override
    public List<CsRevocationRegulationListItemVO> list() {
        List<CsRevocationRegulationListItemVO> list = mapper.list();
        list.removeIf(hall -> hall.getState() == StateCode.STATE_DELETE.getValue());
        return list;
    }

    @Override
    public List<CsRevocationRegulationListItemVO> search(String connect) {
        List<CsRevocationRegulationListItemVO> list = mapper.search(connect);
        return list;
    }
}
